﻿using Asp.Versioning;
using FlyingEye.DeviceServices;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace FlyingEye.Controllers
{
    /// <summary>
    /// 设备配置控制器
    /// </summary>
    [Authorize]
    [ControllerName("DeviceClient")]
    [ApiVersion("1.0")]
    [Route("api/device-app-service/v{version:apiVersion}/device-clients")]
    public class DeviceClientController : DefaultController
    {
        private readonly DeviceClientService _deviceClientService;

        public DeviceClientController(DeviceClientService deviceClientService)
        {
            _deviceClientService = deviceClientService;
        }

        /// <summary>
        /// 获取一个客户端
        /// </summary>
        [HttpGet("{id}", Name = "GetByDeviceClientId")]
        public async Task<ActionResult<DeviceClientResponse>> GetAsync(string id)
        {
            return await this.GetInvokeAsync(async () =>
            {
                return await _deviceClientService.GetAsync(id);
            });
        }

        [HttpGet]
        public async Task<ActionResult<List<DeviceClientResponse>>> GetListAsync()
        {
            return await this.GetInvokeAsync(async () =>
            {
                return await _deviceClientService.GetListAsync();
            });
        }

        [HttpDelete("{id}")]
        public async Task<NoContentResult> DeleteAsync(string id)
        {
            return await this.DeleteInvokeAsync(async () =>
            {
                await _deviceClientService.DeleteAsync(id);
            });
        }

        [HttpPost]
        public async Task<ActionResult<DeviceClientResponse>> InsertAsync([FromBody] CreateDeviceClientRequest deviceClient)
        {
            return await this.InsertInvokeAsync(async () =>
            {
                return await _deviceClientService.InsertAsync(deviceClient);
            }, "GetByDeviceClientId", e => new { id = e.Id });
        }

        [HttpPut]
        public async Task<ActionResult<DeviceClientResponse>> UpdateAsync([FromBody] UpdateDeviceClientRequest deviceClient)
        {
            return await this.PutInvokeAsync(async () =>
            {
                return await _deviceClientService.UpdateAsync(deviceClient);
            });
        }

        [HttpGet("{id}/configurations")]
        public async Task<ActionResult<List<DeviceConfigurationResponse>>> GetConfigurationsAsync(string id)
        {
            return await this.GetInvokeAsync(async () =>
            {
                return await _deviceClientService.GetConfigurationsAsync(id);
            });
        }
    }
}
